﻿using BitButterECS.Event.EventArgs;
using System.Collections.Generic;

namespace BitButterECS.Event
{
    public abstract class BaseEvent<T> where T : BaseEventArgs
    {
		public BaseEvent(T e)
		{
			EventArgs = e;
		}

		T EventArgs { get; }

		public virtual void RaiseEvent(object sender)
		{
			foreach (EventHandler handler in Handlers)
			{
				handler.Invoke(sender, EventArgs);
			}
		}

		static List<EventHandler> Handlers
		{
			get
			{
				if (handlers == null)
				{
					handlers = new List<EventHandler>();
				}
				return handlers;
			}
		}
		static List<EventHandler> handlers;

		public delegate void EventHandler(object sender, T e);
		public static event EventHandler Handler
		{
			add
			{
				Handlers.Add(value);
			}
			remove
			{
				Handlers.Remove(value);
			}
		}

		public static void RaiseEvent(object sender, T e)
		{
			foreach (EventHandler handler in Handlers)
			{
				handler.Invoke(sender, e);
			}
		}
	}
}
